package com.hmall.item.web;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    //分页查询
    @GetMapping("/list")
    public PageDTO<Item> selectByPage(@RequestParam("page") Integer page, @RequestParam("size") Integer size){
        Page<Item> itemPage = itemService.page(new Page<>(page, size));
        return new PageDTO<>(itemPage.getTotal(),itemPage.getRecords());
    }

    //根据商品id查询
    @GetMapping("/{id}")
    public Item selectById(@PathVariable Long id){
        Item item = itemService.getById(id);
        return item;
    }

    //新增商品
    @PostMapping()
    public void addItems(@RequestBody Item item){
      itemService.save(item);
    }

    //商品的上下架
    @PutMapping("/status/{id}/{status}")
    public void UpAndDownShelves(@PathVariable Long id,@PathVariable Integer status){
        itemService.UpAndDownShelves(id,status);
    }

    //商品的修改
    @PutMapping
    public void updateItems(@RequestBody Item item){
        itemService.saveOrUpdate(item);
    }

    //根据id删除商品
    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable Long id){
           itemService.removeById(id);
    }


}
